import Taro from "@tarojs/taro";

export class InnerAudioContextManger{

    private _context?:Taro.InnerAudioContext
    private readonly _useWebAudioImplement: boolean;

    constructor(useWebAudioImplement:boolean = false) {
        this._useWebAudioImplement = useWebAudioImplement;
    }

    private get context(){
        if (!!this._context) return this._context
        let innerAudioContext = Taro.createInnerAudioContext({
            useWebAudioImplement: this._useWebAudioImplement
        });
        innerAudioContext.onError(error=>{
            console.error(error)
        })
        return innerAudioContext
    }

    set src(src:string){
        this.context.src = src
    }

    play(src?:string){
        if (src){
            this.src = src
        }
        this.context.play()
    }

    pause(){
        this.context.pause()
    }

    stop(){
        this.context.stop()
    }

    destory(){
        this.context.destroy()
        this._context = undefined
    }

}